package com.example.myandroidtest.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myandroidtest.R;
import com.example.myandroidtest.bean.MenuBean;

import java.util.List;

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuHolder> {

    private final List<MenuBean> listData;
    private final OnRecyclerViewItemClickListener onItemClickListener;

    public MenuAdapter(List<MenuBean> listData,
                       OnRecyclerViewItemClickListener onItemClickListener) {
        this.listData = listData;
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public MenuHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_menu, parent,
                false);
        return new MenuHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MenuHolder holder, int position) {
        MenuBean itemData = listData.get(position);
        holder.tvName.setText(itemData.getName());
        holder.itemView.setOnClickListener((View v) -> onItemClickListener.onItemClick(holder,
                position));
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public static class MenuHolder extends RecyclerView.ViewHolder {
        TextView tvName;

        public MenuHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
        }
    }
}
